<template>
    <el-dialog :title="form.id?'编辑角色':'新建角色'" width="600px" :visible.sync="dialogVisible">
        <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
            <el-form-item prop="name" label="角色名称">
                <el-input v-model="form.name" type="text" placeholder="请输入" style="width: 100%;" />
            </el-form-item>
            <el-form-item prop="type" label="角色类型">
                <el-radio-group v-model="form.type">
                    <el-radio label="1">管理员</el-radio>
                    <el-radio label="2">员工</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item prop="sort" label="排序">
                <el-input v-model="form.sort" type="text" placeholder="请输入" style="width: 100%;" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { saveGroup, departList } from '@/api/system'
import { deepClone } from '@/util'
export default {
    name: 'ResvervationAddDialog',
    data() {
        return {
            dialogVisible: false,
            form: {
                name: '',
                type: '',
                sort: ''
            },
            authOptions: [
                { label: '总管理员', value: '1' },
                { label: '管理员', value: '2' },
                { label: '员工', value: '3' }
            ],
            departOptions: [],
            rules: {
                name: [{ required: true, message: '请输入姓名', trigger: 'change' }],
                type: [{ required: true, message: '请选择类型', trigger: 'change' }],
                sort: [{ required: true, message: '请输入排序', trigger: 'change' }]
            }
        }
    },
    methods: {
        showDialog(item) {
            this.dialogVisible = true;
            this.resetForm();
            if (item && item.id) {
                this.$nextTick(() => {
                    this.form = deepClone(item);
                })
            }
        },
        resetForm() {
            this.$nextTick(() => {
                this.$refs.ruleForm.resetFields();
            })
        },
        resetParams() {
            this.form = {
                name: '',
                type: '',
                sort: ''
            };
            
        },
        handleSubmit(e) {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    saveGroup(this.form).then(res => {
                        if (res.status == '1') {
                            this.$message.success('操作成功！')
                            this.$emit('success', res)
                        } else {
                            this.$message.error(res.message)
                        }
                    }).finally(() => {
                        this.dialogVisible = false
                    })
                }
            })
        },
        refreshData(val) {
            this.$forceUpdate()
        }
    }
}
</script>